﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using VitCode.BaseDataPageCreator;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;
using VitCode.WorkFlow.DataAccess;
using VitCode.WorkFlow.Entities;

namespace AMS.Web.WorkBill
{
    public partial class WoSelectUsers : UserControl
    {
        private const string SN_ITEM_OBJECT = "ItemObjects";

        private const string SN_WORK_ITEM = "WorkItem";

        private const string SN_SELECT_USER_KEYS = "SelectUserKeys";

        public NameObjectDictionary ItemObjects
        {
            get
            {
                object obj = SessionObjectHelper.GetSession(Page, ClientID + SN_ITEM_OBJECT);
                if (obj != null && obj is NameObjectDictionary)
                    return ((NameObjectDictionary) obj);
                return new NameObjectDictionary();
            }
            set { SessionObjectHelper.SetSession(Page, ClientID + SN_ITEM_OBJECT, value); }
        }

        public WorkItem WorkItem
        {
            get
            {
                object obj = SessionObjectHelper.GetSession(Page, ClientID + SN_WORK_ITEM);
                if (obj != null && obj is WorkItem)
                    return ((WorkItem) obj);
                return null;
            }
            set { SessionObjectHelper.SetSession(Page, ClientID + SN_WORK_ITEM, value); }
        }

        public StringCollection SelectUserKeys
        {
            get
            {
                object obj = SessionObjectHelper.GetSession(Page, ClientID + SN_SELECT_USER_KEYS);
                if (obj != null && obj is StringCollection)
                    return ((StringCollection) obj);
                return new StringCollection();
            }
            set { SessionObjectHelper.SetSession(Page, ClientID + SN_SELECT_USER_KEYS, value); }
        }

        public NameObjectDictionary GetSelectUsers()
        {
            var nod = new NameObjectDictionary();
            List<ListItem> items = (from ListItem c in lstUsers.Items where c.Selected orderby c.Text select c).ToList();
            foreach (ListItem item in items)
            {
                if (!nod.Contains(item.Value))
                    nod.Add(item.Value, ItemObjects[item.Value]);
            }
            return nod;
        }

        private void InitItemObjects(WorkItem wi)
        {
            if (wi != null && wi.OperateRole != null)
            {
                var xmlEnumHlp = new XmlEnumHelper();

                lblName.Text = wi.Name;
                lblFixType.Text = xmlEnumHlp.GetItemName("WoFixType", wi.FixType);
                var acc = new WorkFlowAccess();
                try
                {
                    string hql = "From " + typeof (User).Name +
                                 " c Where c.Id in (select a.UserId From " + typeof (UserRoleRelation).Name +
                                 " Where a.RoleId='')";
                    IList lst = acc.Session.CreateQuery(hql).List();
                    ItemObjects.Clear();
                    foreach (User u in lst)
                    {
                        ItemObjects.Add(u.Code, u);
                    }
                }
                finally
                {
                    acc.CloseSession();
                }
            }
        }

        private void SetData()
        {
            List<string> keys = (from String c in ItemObjects.Keys orderby c select c).ToList();
            lstUsers.Items.Clear();
            foreach (string t in keys)
            {
                var obj = ((User) ItemObjects[t]);
                var item = new ListItem(obj.Name, t);
                item.Selected = SelectUserKeys.Contains(t);
                lstUsers.Items.Add(item);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitItemObjects(WorkItem);
            }
            SetData();
        }
    }
}